<template>
    <section>
<!--        <p class="login_redirect">没有帐号，去<span @click="registerBtn">注册</span></p>-->
        <div>
            <p class="login_title">欢迎登录</p>
            <form ref="loginForm" class="login_form" @keydown.enter="handleSubmit">
                <div class="login_formItem">
                    <input placeholder="输入手机号" class="" v-model.trim="form.username">
                </div>
                <div class="login_formItem">
                    <input placeholder="输入密码" class="" type="password" v-model.trim="form.password">
                </div>
                <div class="login_submitButtonContainer" style="margin-top: 34px; height: 46px;">
                    <el-button type="info" class="login_button" @click="handleSubmit" :loading="loginLoading">登录</el-button>
                </div>
<!--                <p class="login_forgot" @click="handleForget">忘记密码?</p>-->
            </form>
        </div>
    </section>
</template>
<script>
    import { localRead } from '@/libs/util'
    import { decrypt } from '@/libs/tools';

    export default {
        name: 'LoginForm',
        props: {
            user: {
                type: Object,
                default: {}
            },
        },
        watch: {
            user(data){
                this.form.username = data.name ? data.name : '';
            }
        },
        data () {
            return {
                form: {
                    username: this.user.name ? this.user.name : '',
                    password: ''
                },
                loginLoading: false,
                rememberPassword: true,
            }
        },
        computed: {

        },
        methods: {
            handleSubmit () {
                this.loginLoading = true;
                this.$emit('on-login', {
                    username: this.form.username,
                    password: this.form.password,
                    remember: this.rememberPassword
                });
            },
            registerBtn(){
                this.$emit('on-register');
            },
            handleForget() {
                this.$emit('on-forget');
            },
            getRememberPassword(){
                let localRes = localRead('rememberPassword');
                localRes = localRes ? JSON.parse(localRes) : {};
                if(localRes.remember){
                    this.form.username = localRes.username;
                    this.form.password = decrypt(localRes.password);
                    this.rememberPassword = localRes.remember;
                }
            },
        },
        mounted: function(){
            this.getRememberPassword();
        }
    }
</script>
<style scoped>

</style>
